package com.buddy.qreport.web.datasource.controller;

import com.buddy.qreport.common.mybatis.entity.Page;
import com.buddy.qreport.datasource.entity.DataSourceEntity;
import com.buddy.qreport.datasource.service.IDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/datasource")
@RestController("QREPORT_DatasourceController")
public class DatasourceController {

    @Autowired
    @Qualifier("QREPORT_DataSourceService")
    private IDataSourceService dataSourceService;

    @GetMapping("/list")
    public ResponseEntity<Page<DataSourceEntity>>list(@RequestBody(required = false) Page page){

        if(page == null){
            page = new Page();
        }

        return new ResponseEntity<>( dataSourceService.getList(page),HttpStatus.OK);
    }

    @GetMapping("/listAll")
    public ResponseEntity<List<DataSourceEntity>>listAll(){

        return new ResponseEntity<>( dataSourceService.getList(),HttpStatus.OK);
    }


    @GetMapping("/getOne")
    public ResponseEntity getOne(@RequestParam(value = "id",required = true) String id){


        DataSourceEntity entity = dataSourceService.getOne(id);

        return new ResponseEntity(entity,HttpStatus.OK);

    }

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody DataSourceEntity entity){



        do{

            if(entity == null){

                break;
            }

            if(StringUtils.isEmpty(entity.getDatasourceName()))
                break;

            if(StringUtils.isEmpty(entity.getDatasourcePassword()))
                break;

            if(StringUtils.isEmpty(entity.getDatasourceUrl()))
                break;

            if(StringUtils.isEmpty(entity.getDatasourceUsername()))
                break;


            dataSourceService.add(entity);


            return new ResponseEntity(HttpStatus.OK);


        }while (false);

        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/test")
    public ResponseEntity test(@RequestParam(required = true,value = "id") String id ){


        Map<String,Object> result = new HashMap<>();



        DataSourceEntity entity = dataSourceService.getOne(id);

        if(entity != null){

            try {

                dataSourceService.test(entity);

                result.put("status",true);
            }catch (Exception e){

                result.put("status",false);
                result.put("message",e.getLocalizedMessage());

            }


        }


        return new ResponseEntity(result,HttpStatus.OK);
    }

    @PostMapping("/update")
    public ResponseEntity update(@RequestBody DataSourceEntity entity){

        if(entity == null){

            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }

        dataSourceService.updateById(entity);


        return new ResponseEntity(HttpStatus.OK);

    }



    @PostMapping("/delete")
    public ResponseEntity delete(@RequestParam(value = "id",required = true) String id){


        dataSourceService.delete(id);


        return new ResponseEntity(HttpStatus.OK);

    }

}
